var companyId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.companyId);
var schoolId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolId);
var schoolMap = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolMap);

var app = new Vue({
    el: '#app',
    data: {
        permissions:['courseClassTime:add'],//本页权限数组
        beginDate: '',
        endDate: '',
        classId: '',
        classList: [],
        timeList: [],
        isChange: 0,
        notifyType: 0,
        msg: '',
        selects: ['周一','周二','周三','周四','周五','周六','周日'],
    },
    //进入页面初始化方法
    created: function () {
        var that = this;
        that.getClassList();
        var token = sessionStorageUtils.getJson(storageKey.sessionStorageKey.token);
        if(empty(token)){
            layer.msg("<span style='color:red'>登录超时！请重新登录！</span>", {time: 1500, icon:5});
            setTimeout(function () {
                window.location.href = _LOGIN_PAGE;
            },1200);

            return ;
        }

    },
    methods: {
        //权限控制
        permission: function (index) {
            return (_PERMISSIONS.indexOf(this.permissions[index]) != -1);
        },
        // 得到班级
        getClassList: function() {
            var that = this;
            Get(that, _HOST + _API.organization_courseClass_getClassBy + "?schoolId="+schoolId+"&companyId="+companyId)
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if (response) {
                        that.classList = response.list;
                    }
                });
        },
        // 选择班级事件
        selectClass: function() {
            var that = this;
            that.timeList = [];
            that.beginDate = '';
            that.endDate = '';

            that.getInfoById(that.classId);
        },
        getChangeInfo: function() {
            var that = this;
            that.isChange = $('input[name="isChange"]').filter(':checked').val();
            that.notifyType = $('input[name="notifyType"]').filter(':checked').val();
            // console.log(that.isChange);
            // console.log(that.notifyType);
        },
        // 添加
        add:function () {
            var that = this;
            that.setTimes();
            that.getSpecList();
            that.getChangeInfo();
            $("#haveChanged").find('input,textarea,select').each(function(){
                var name = $(this).attr("name");
                if (name == 'msg') {
                    that.msg = $(this).val();
                }
            });
            if (empty(that.classId)) {
                layer.msg("请选择班级", {time: 1500, icon:5});
                return;
            }
            var param = {};
            param.isChange = that.isChange;
            param.notifyType = that.notifyType;
            param.msg = that.msg;
            param.courseClassTimeList = that.timeList;
            console.log(param);
            Post(that,_HOST+_API.organization_courseClassTime_add,param,true,that.permissions[0])
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if(response){
                        if(response.success){
                            layer.msg(response.msg, {time: 1500, icon:1});
                            setTimeout(function () {
                                window.parent.location.reload(); //刷新父页面
                                layer.closeAll();
                            }, 1500)
                        }else {
                            layer.msg(response.msg, {time: 1500, icon:5});
                        }
                    }
                })
        },
        getInfoById:function (id) {
            var that = this;
            Get(that, _HOST + _API.organization_courseClassTime_getById + "?id=" + id)
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if (response) {
                        if (response.success) {
                            var list = response.list;
                            // 填入数据
                            if (!empty(list)) {
                                that.beginDate = list[0].beginDate;
                                that.endDate = list[0].endDate;
                                $("#Form select[name='isJumpOver']").val(list[0].isJumpOver);
                                that.timeList = list;
                            }
                        } else {
                            layer.msg(response.msg, {time: 1500, icon: 5});
                        }
                    }
                })
        },
        // 设置时间
        setTimes: function () {
            var that = this;
            $(".time").each(function(){
                var name = $(this).attr("name");
                if (name == 'beginDate') {
                    that.beginDate = $(this).val();
                } else if (name == 'endDate') {
                    that.endDate = $(this).val();
                }
            });
        },
        // 时间相关
        addSpec: function () {
            var that = this;
            layer.open({
                type: 1,
                content: $('#select')
            });
        },
        delSpec: function(index) {
            var that = this;
            that.setTimes();
            that.timeList.splice(index,1);
        },
        getSpecList: function () {
            var that = this;
            var list = that.timeList;
            for (let i = 0; i < list.length; i++) {
                list[i].courseClassId = that.classId;
                list[i].beginDate = that.beginDate;
                list[i].endDate = that.endDate;
                list[i].isJumpOver = $($('select[name="isJumpOver"]')[0]).val();
                list[i].companyId = companyId;
                list[i].schoolId = schoolId;
                list[i].beginTime = $($('input[name="beginTime"]')[i]).val();
                list[i].endTime = $($('input[name="endTime"]')[i]).val();
            }
            that.timeList = list;
            console.log(that.timeList);
        },
        addTimes: function () {
            layer.closeAll();
            var that = this;
            that.setTimes();
            that.timeList = [];
            var weeks = $('input[name="weeks"]').filter(':checked').val();
            $('input[name="weeks"]:checked').each(function(index,element){
                that.timeList.push({id:'',weeks:$(this).val(),beginTime:'',endTime:''});
            });
        },
    },
});

Validator();
function Validator() {
    $("#Form").submit(function(e){
        e.preventDefault();
        layer.open({
            type: 1,
            content: $('#haveChanged')
        });
    });
}
